<?Php

class mdata extends CI_Model {
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
		
	function get_data($par1="",$par2="",$p3="",$p4="",$p5=""){
		$where =' WHERE 1=1 ';
		switch ($par1){
			case "user_aplikasi":
				$sql 	= "SELECT * FROM user_aplikasi where user_id='".$par2."'";
				$result = $this->db->query($sql)->result_array();	
			break;
			case "slot_propinsi_kab":
				$sql 	= "SELECT * FROM jenis_data WHERE id = '".$par2."'"	;
				$result = $this->db->query($sql)->row();
			break;
			
			case "data_grafik":
				$join="";
				$id_prov=$this->input->post('prov');
				if($par2 != ""){
					$where .=" AND B.id_parent=".$par2;
				}else{
					$where .= "";
				}
				
				if($p3 != ""){
					$where .=" AND B.id_jenis = ".$p3." AND B.id_parent is null ";
				}else{
					$where .= " ";
				}
				
				if($id_prov!=0){
					$join .=" LEFT JOIN tbl_kabkota C ON A.id_user=C.id ";
					$where .=" AND C.id_tbl_provinsi=".$id_prov;
				}
				if($this->input->post('tahun')){$where .=" AND A.tahun=".$this->input->post('tahun');}
				$sql="SELECT B.nama,SUM(A.nilai)/1000000 AS NILAI 
						FROM data A
						LEFT JOIN item_data B ON A.id_item=B.id
						".$join.$where."						 
						GROUP BY B.nama";
				//echo $sql;
					$result = $this->db->query($sql)->result_array();
			break;
			
		}
		return $result;
	}
	
	function get_jsonna($sql){
		$page = (integer) (($this->input->post('page')) ? $this->input->post('page') : "1");
		$limit = (integer) (($this->input->post('rows')) ? $this->input->post('rows') : "15");
		 
		$count = $this->db->query($sql)->num_rows();
		
		if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages; 
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if($start<0) $start=0;
		  
		$sql = $sql . " LIMIT $start,$limit";
		
		 $data=$this->db->query($sql)->result();  
			if($total_pages==0){
				$kos='{"total":0,"rows":[]}';
				return $kos;
			}
			else{  	
				if($data){
				   $responce->rows= $data;
				   $responce->total =$count;
				   return json_encode($responce);
				}else{ 
				   return "";
				}   
			}
 	}
	
	function fillCombo($req, $val='-1'){
		switch($req){
			case 'prov':
				$sql = "SELECT nama_provinsi as txt,id FROM tbl_provinsi";
				break;
			
			case 'kat':
				$sql = "SELECT id,nama as txt FROM item_data WHERE id_jenis='".$this->input->post('v')."' AND id_parent IS NULL ";
				break;
		}
		
		return $this->db->query($sql)->result_array();	
	}
	
	function isi_combo($table,$field_id,$field_display){
		$sql="select ".$field_id." as id ,".$field_display." as text from ".$table;
		return $this->db->query($sql)->result_array();
	}
		
	function simpan($p1="",$post=""){
		$this->db->trans_begin();
		$id=$this->input->post("id");
		$sts=$post["sts"];
		unset ($post["sts"]);
		switch ($p1){
			case "tbl_faq":
				if($sts=='edit'){
					$this->db->where('id',$id);	
				}
			break;
		}
		
		if($sts=='add'){
			$post["tgl_buat"]=date('Y-m-d');
			$this->db->insert($p1,$post);
		}
		
		else{
			$post["update_date"]=date('Y-m-d');
			$this->db->update($p1,$post);
		}
		
		if($this->db->trans_status() == false){
		$this->db->trans_rollback();
		return "Data not saved";
		} else{
		return $this->db->trans_commit();
		}
		
		
	}
	
		function changePass($userId, $newPass){
		$sql = "UPDATE USER_APLIKASI SET USER_PASS = '$newPass' WHERE USER_ID = '$userId'";
		
		$this->db->query($sql);
	}
	
	
}